/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eschool.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.eschool.model.Course;
import com.eschool.model.Teacher;
import com.eschool.util.DateFormaterUtil;
import com.eschool.util.HibernateUtil;
import com.eschool.util.JdbcUtil;

/**
 *
 * @author Ammar
 */

public class TeacherDao{
	
	public static int addTeacher(Teacher teacher) {
    	int teacherId = -1;
    	
    	Connection connection = JdbcUtil.openConnection();
		try{
			CallableStatement storedProc = connection.prepareCall("{ call InsertTeacher(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
			
			storedProc.setString(1, teacher.getName());
			storedProc.setString(2, teacher.getStaffNumber());
			storedProc.setString(3, teacher.getCNIC());
			storedProc.setBoolean(4, teacher.isGender());
			storedProc.setString(5, DateFormaterUtil.format(DateFormaterUtil.SQL_DATE,teacher.getDob()));
			storedProc.setString(6, teacher.getPosition());
			storedProc.setString(7, teacher.getMobileNumber());
			storedProc.setString(8, teacher.getPhoneNumber());
			storedProc.setBoolean(9, teacher.isCurrentStatus());
			storedProc.setString(10, teacher.getPresentAddress());
			storedProc.setString(11, teacher.getPermanentAddress());
			storedProc.setString(12, teacher.getCv());
			storedProc.setInt(13, teacher.getUser().getUserId());
			storedProc.setInt(14, teacher.getBranch().getBranchId());
			storedProc.registerOutParameter(15, java.sql.Types.INTEGER);

			storedProc.execute();
		
			teacherId = storedProc.getInt(15);
			
			storedProc = connection.prepareCall("{ call InsertTeacherCourses(?,?) }");
			
			for (Iterator iterator = teacher.getCourses().iterator(); iterator.hasNext();) {
				Course course = (Course) iterator.next();
				storedProc.setInt(1, teacherId);
				storedProc.setInt(2, course.getCourseId());
				
				storedProc.execute();
			}
			
			
			//System.out.print("Done");
			
			connection.close();
		}
		
		catch(Exception e){
			teacherId = -1;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return teacherId;
		}
		
	}
	
	public static Teacher getTeacherByProperty(String property, Object value) {
		
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(Teacher.class);
		crit.add(Restrictions.eq(property, value));
		
		Teacher teacher = (Teacher) crit.uniqueResult();
		
        HibernateUtil.closeSession(session);

        return teacher;
	}
	
	public static List getTeachers(Map<String , Object> conditions) {
		
		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(Teacher.class);
		
		if (conditions != null)
			for (Iterator iterator = conditions.entrySet().iterator(); iterator.hasNext();) {
				Map.Entry condition = (Map.Entry) iterator.next();
				crit.add(Restrictions.eq(((String) condition.getKey()), condition.getValue()));
			}
		
		List teachers = (List<Teacher>) crit.list();
		
        HibernateUtil.closeSession(session);
        
        if(teachers == null)
        	return Collections.EMPTY_LIST;

        return teachers;
	}
	
	public static boolean updateTeacher(Teacher teacher) {
    	boolean isUpdated = false;
    	
    	Connection connection = JdbcUtil.openConnection();
		try{
			CallableStatement storedProc = connection.prepareCall("{ call spTeacherUpdate(?,?,?,?,?,?,?,?,?,?,?,?) }");
			
			storedProc.setString(1, teacher.getName());
			storedProc.setString(2, teacher.getStaffNumber());
			storedProc.setString(3, teacher.getCNIC());
			storedProc.setString(4, DateFormaterUtil.format(DateFormaterUtil.SQL_DATE,teacher.getDob()));
			storedProc.setString(5, teacher.getPosition());
			storedProc.setString(6, teacher.getMobileNumber());
			storedProc.setString(7, teacher.getPhoneNumber());
			storedProc.setBoolean(8, teacher.isCurrentStatus());
			storedProc.setString(9, teacher.getPresentAddress());
			storedProc.setString(10, teacher.getPermanentAddress());
			storedProc.setString(11, teacher.getCv());
			storedProc.setInt(12, teacher.getTeacherId());
			
			storedProc.execute();
			
			isUpdated = true;
			
			connection.close();
		}
		
		catch(Exception e){
			isUpdated = false;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return isUpdated;
		}	
	}
}
